package com.leap.meeting.view;

import com.leap.meeting.dao.BaseDAO;
import com.leap.meeting.dao.imple.AdminDAOimple;
import com.leap.meeting.dao.imple.RoomDAOimple;
import com.leap.meeting.dao.imple.UserDAOimple;
import com.leap.meeting.pojo.Admin;
import com.leap.meeting.pojo.Room;
import com.leap.meeting.pojo.User;
import com.leap.meeting.util.DataUtil;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.sql.ResultSet;
import java.sql.SQLException;


public class Main_Admin extends JFrame implements MouseListener {

    //设置窗口主题样式
    static {
        String lookAndFeel = "com.sun.java.swing.plaf.windows.WindowsLookAndFeel";
        try {
            UIManager.setLookAndFeel(lookAndFeel);
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        } catch (InstantiationException e) {
            throw new RuntimeException(e);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        } catch (UnsupportedLookAndFeelException e) {
            throw new RuntimeException(e);
        }
    }

    static private AdminDAOimple adminDAOimple = new AdminDAOimple();
    static private UserDAOimple userDAOimple = new UserDAOimple();
    static private RoomDAOimple roomDAOimple = new RoomDAOimple();
    static private Admin admin;
    static private BaseDAO baseDAO = new BaseDAO();

    Font f1 = new Font("微软雅黑", Font.BOLD,30);
    Font f2 = new Font("微软雅黑", Font.PLAIN, 15);
    Font f3 = new Font("微软雅黑", Font.PLAIN, 18);
    Font f4 = new Font("微软雅黑", Font.PLAIN, 12);

    JTabbedPane pane = new JTabbedPane();
    JPanel p1 = new JPanel();
    JPanel p2 = new JPanel();
    JPanel p3 = new JPanel();
    JPanel p4 = new JPanel();
    JPanel p5 = new JPanel();

    JTable table1;
    JTable table3;
    JTable table4;
    JTable table5;

    //p1组件
    JButton p1_button1 = new JButton("通过");
    JButton p1_button2 = new JButton("不通过");
    JButton p1_button3 = new JButton("查询");
    JTextField p1_text = new JTextField();
    JScrollPane scrollPane1 = new JScrollPane();
    String p1_sql = "SELECT real_name AS realName,name,address,seat_num AS seatNum,reservations.status AS status from reservations,rooms,user where reservations.room_id = rooms.id AND reservations.user_id = user.id";
    String p1_sql2 = "SELECT id,name,address,seat_num AS seatNum,status from rooms where name LIKE ?";
    String p1_sql3 = "UPDATE reservations SET status = '通过' where user_id = ? AND room_id = ?";
    String p1_sql4 = "UPDATE record SET express = 1 WHERE speaker = ? AND room_name = ?";
    String p1_sql5 = "UPDATE reservations SET status = '不通过' where user_id = ? AND room_id = ?";
    String p1_sql6 = "UPDATE record SET express = 2 WHERE speaker = ? AND room_name = ?";

    private String[] p1_head={"预约人","会议室","地点","人数","审核状态"};

    //p2组件
    JTextField account = new JTextField();
    JTextField name = new JTextField();
    JTextField phone = new JTextField();
    JTextField email = new JTextField();
    JTextField password = new JTextField();
    JButton save = new JButton("保存");
    JButton quit = new JButton("退出登录");

    //p3组件
    JTextField p3_text = new JTextField();
    JButton p3_button1 = new JButton("查询");
    JButton p3_button2 = new JButton("全选");
    JButton p3_button3 = new JButton("删除");
    Integer p3_num1 = 0;
    Integer p3_num2 = 0;
    Integer p3_num3 = 0;
    Integer p3_num4 = 0;
    JLabel p3_label3 = new JLabel("总用户数: "+p3_num1);
    JLabel p3_label4 = new JLabel("可用会议室数量: "+p3_num2);
    JLabel p3_label5 = new JLabel("预约数量: "+p3_num3);
    JLabel p3_label6 = new JLabel("待审核数: "+p3_num4);
    JScrollPane scrollPane3 = new JScrollPane();
    private String[] p3_head ={"Id","会议室名称","地点","参会人数","主持人","会议主题","开始时间","结束时间","会议内容"};
    String p3_sql = "SELECT id,room_name AS name,address,attendees,speaker,topic,start_time,end_time,training_content from record where express = 1";
    String p3_sql2 = "SELECT id,room_name AS name,address,attendees,speaker,topic,start_time,end_time,training_content from record where room_name LIKE ? AND express = 1";
    String p3_sql3 = "DELETE FROM record where id = ?";
    String p3_sql4 = "SELECT COUNT(*) FROM user";
    String p3_sql5 = "SELECT COUNT(*) FROM rooms WHERE status = '空闲'";
    String p3_sql6 = "SELECT COUNT(*) FROM reservations";
    String p3_sql7 = "SELECT COUNT(*) FROM reservations WHERE status = '审核中'";

    //p4组件
    JDialog insertJDialog;
    JScrollPane scrollPane4 = new JScrollPane();
    JTextField p4_text = new JTextField();
    JButton p4_button1 = new JButton("查询");
    JButton p4_button2 = new JButton("新增");
    JButton p4_button3 = new JButton("删除");
    JTextField accountField = new JTextField();
    JTextField passwordField = new JTextField();
    JTextField realNameField = new JTextField();
    JTextField apartmentField = new JTextField();
    JTextField dutyField = new JTextField();
    JTextField phoneField = new JTextField();
    JTextField emailField = new JTextField();
    JButton yes = new JButton("新增");
    JButton no = new JButton("取消");
    JLabel accountRule = new JLabel("账号至少三位");
    JLabel passwordRule = new JLabel("密码至少三位");
    JLabel phoneRule =new JLabel("请输入正确的手机号！");
    JLabel emailRule= new JLabel("请输入正确的邮箱！");
    private String[] p4_head = {"Id","账号","密码","姓名","部门","手机号","邮箱","职位"};
    String p4_sql = "SELECT id,account,password,real_name AS realName,apartment,phone,email,duty FROM user";
    String p4_sql2 = "SELECT id,account,password,real_name AS realName,apartment,phone,email,duty FROM user WHERE real_name LIKE ?";
    String p4_sql3 = "DELETE FROM user WHERE id = ?";

    //p5组件
    JDialog addRoomJDialog;
    JScrollPane scrollPane5 = new JScrollPane();
    JTextField p5_text = new JTextField();
    JButton p5_button1 = new JButton("查询");
    JButton p5_button2 = new JButton("设置为空闲");
    JButton p5_button3 = new JButton("设置为占有");
    JButton p5_button4 = new JButton("新增");
    JButton room_yes = new JButton("确定");
    JButton room_no = new JButton("取消");
    JTextField roomNameField = new JTextField();
    JTextField addressField = new JTextField();
    JTextField seatNumField = new JTextField();
    JTextField statusField = new JTextField("空闲");
    String p5_sql = "SELECT id,name,address,seat_num AS seatNum,status from rooms";
    String p5_sql2 = "SELECT id,name,address,seat_num AS seatNum,status from rooms WHERE name LIKE ?";
    String p5_sql3 = "UPDATE rooms SET status = '空闲' where id = ?";
    String p5_sql4 = "UPDATE rooms SET status = '占有' where id = ?";
    private String[] p5_head={"Id","会议室名称","地点","座位数","状态"};

    public Main_Admin(String acc) {
        InitJFrame();
        InitTable(acc);
        this.setVisible(true);
    }

    private void InitJFrame() {
        this.setTitle("会议管理系统——管理员");
        this.setSize(900, 550);
        this.setLocationRelativeTo(null);//设置窗口居中
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setAlwaysOnTop(true);
        this.setResizable(false);//设置不可以通过拖动边框随意改变窗口大小

        //p1面板中的组件
        scrollPane1.setBounds(10, 60, 700, 400);

        JLabel p1_label1 = new JLabel("会议室名称");

        p1_label1.setBounds(300, 30, 80, 25);
        p1_button1.setBounds(720, 100, 150, 40);
        p1_button2.setBounds(720, 180, 150, 40);
        p1_button3.setBounds(620, 30, 80, 25);
        p1_button1.addMouseListener(this);
        p1_button2.addMouseListener(this);
        p1_button3.addMouseListener(this);
        p1_text.setBounds(400, 30, 200, 25);

        p1_button1.setFont(f2);
        p1_button2.setFont(f2);
        p1_button3.setFont(f2);
        p1_text.setFont(f2);
        p1_label1.setFont(f2);

        p1.setLayout(null);
        p1.add(p1_button1);
        p1.add(p1_button2);
        p1.add(p1_button3);
        p1.add(p1_text);
        p1.add(scrollPane1);
        p1.add(p1_label1);

        //p2面板中的组件
        JLabel p2_title = new JLabel("个人信息");
        JLabel account_label = new JLabel("账号");
        JLabel name_label = new JLabel("姓名");
        JLabel phone_label = new JLabel("手机号");
        JLabel email_label = new JLabel("邮箱");
        JLabel password_label = new JLabel("密码");

        p2_title.setBounds(350, 10, 200, 50);
        account_label.setBounds(150,80,100,25);
        name_label.setBounds(150,130,100,25);
        phone_label.setBounds(150,180,100,25);
        email_label.setBounds(150,230,100,25);
        password_label.setBounds(150,280,100,25);

        account.setBounds(220, 80, 200, 25);
        name.setBounds(220, 130, 200, 25);
        phone.setBounds(220, 180,200, 25);
        email.setBounds(220, 230, 200, 25);
        password.setBounds(220, 280,200, 25);

        p2_title.setFont(f1);
        account_label.setFont(f3);
        name_label.setFont(f3);
        phone_label.setFont(f3);
        email_label.setFont(f3);
        password_label.setFont(f3);

        account.setFont(f2);
        name.setFont(f2);
        phone.setFont(f2);
        email.setFont(f2);
        password.setFont(f2);

        account.setEditable(false);
        name.setEditable(false);
        phone.setEditable(false);
        email.setEditable(false);
        password.setEditable(false);

        JButton edit = new JButton("修改");

        edit.setBounds(600, 100, 150, 30);
        save.setBounds(600, 200, 150, 30);
        quit.setBounds(600, 280, 150, 30);
        save.addMouseListener(this);
        quit.addMouseListener(this);
        edit.setFont(f3);
        save.setFont(f3);
        quit.setFont(f3);
        save.setEnabled(false);

        p2.setLayout(null);
        p2.add(p2_title);
        p2.add(account_label);
        p2.add(name_label);
        p2.add(phone_label);
        p2.add(email_label);
        p2.add(password_label);

        p2.add(account);
        p2.add(name);
        p2.add(phone);
        p2.add(email);
        p2.add(password);

        p2.add(edit);
        p2.add(save);
        p2.add(quit);

        edit.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                account.setEditable(true);
                name.setEditable(true);
                phone.setEditable(true);
                email.setEditable(true);
                password.setEditable(true);
                edit.setEnabled(false);
                save.setEnabled(true);
            }
        });

        save.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                account.setEditable(false);
                name.setEditable(false);
                phone.setEditable(false);
                email.setEditable(false);
                password.setEditable(false);
                edit.setEnabled(true);
                save.setEnabled(false);
            }
        });

        //p3面板中的组件
        JLabel p3_label1 = new JLabel("会议室名称");
        JLabel p3_label2 = new JLabel("数据统计");

        p3_text.setBounds(400, 30, 200, 25);
        p3_button1.setBounds(620, 30, 80, 25);
        p3_button2.setBounds(20, 30, 80, 25);
        p3_button3.setBounds(120, 30, 80, 25);
        p3_button1.addMouseListener(this);
        p3_button2.addMouseListener(this);
        p3_button3.addMouseListener(this);
        p3_label1.setBounds(300, 30, 80, 25);
        p3_label2.setBounds(750, 50, 150, 40);
        p3_label3.setBounds(720, 100, 150, 25);
        p3_label4.setBounds(720,160,150,25);
        p3_label5.setBounds(720,220,150,25);
        p3_label6.setBounds(720,280,150,25);

        p3_text.setFont(f2);
        p3_button1.setFont(f2);
        p3_button2.setFont(f2);
        p3_button3.setFont(f2);
        p3_label1.setFont(f2);
        p3_label2.setFont(f3);
        p3_label3.setFont(f2);
        p3_label4.setFont(f2);
        p3_label5.setFont(f2);
        p3_label6.setFont(f2);

        scrollPane3.setBounds(10, 60, 700, 400);

        p3.setLayout(null);
        p3.add(scrollPane3);
        p3.add(p3_text);
        p3.add(p3_button1);
        p3.add(p3_button2);
        p3.add(p3_button3);
        p3.add(p3_label1);
        p3.add(p3_label2);
        p3.add(p3_label3);
        p3.add(p3_label4);
        p3.add(p3_label5);
        p3.add(p3_label6);

        //p4面板中的组件
        JLabel p4_label = new JLabel("用户名称");

        p4_text.setBounds(400, 30, 200, 25);
        p4_button1.setBounds(620, 30, 80, 25);
        p4_button2.setBounds(720, 100, 150, 40);
        p4_button3.setBounds(720, 180, 150, 40);
        p4_label.setBounds(300, 30, 80, 25);
        p4_text.setFont(f2);
        p4_button1.setFont(f2);
        p4_button2.setFont(f2);
        p4_button3.setFont(f2);
        p4_label.setFont(f2);

        p4_button1.addMouseListener(this);
        p4_button2.addMouseListener(this);
        p4_button3.addMouseListener(this);
        yes.addMouseListener(this);
        no.addMouseListener(this);

        scrollPane4.setBounds(10, 60, 700, 400);

        p4.setLayout(null);
        p4.add(scrollPane4);
        p4.add(p4_text);
        p4.add(p4_button1);
        p4.add(p4_button2);
        p4.add(p4_button3);
        p4.add(p4_label);

        //p5面板中的组件
        JLabel p5_label = new JLabel("会议室名称");

        p5_label.setBounds(300, 30, 80, 25);
        p5_text.setBounds(400, 30, 200, 25);
        p5_button1.setBounds(620, 30, 80, 25);
        p5_button2.setBounds(720, 100, 150, 40);
        p5_button3.setBounds(720, 180, 150, 40);
        p5_button4.setBounds(10,20,150,30);
        p5_label.setFont(f2);
        p5_text.setFont(f2);
        p5_button1.setFont(f2);
        p5_button2.setFont(f2);
        p5_button3.setFont(f2);
        p5_button4.setFont(f2);

        p5_button1.addMouseListener(this);
        p5_button2.addMouseListener(this);
        p5_button3.addMouseListener(this);
        p5_button4.addMouseListener(this);

        scrollPane5.setBounds(10, 60, 700, 400);

        p5.setLayout(null);
        p5.add(scrollPane5);
        p5.add(p5_label);
        p5.add(p5_button1);
        p5.add(p5_button2);
        p5.add(p5_button3);
        p5.add(p5_button4);
        p5.add(p5_text);

        pane.add("审核会议",p1);
        pane.add("个人信息",p2);
        pane.add("会议室记录",p3);
        pane.add("用户管理",p4);
        pane.add("会议室管理",p5);

        this.add(pane);

    }

    private void InitTable(String acc) {
        //p1组件
        ResultSet p1_resultSet;
        try {
            p1_resultSet = DataUtil.querySet(p1_sql, null);
            table1 = new JTable(DataUtil.getArrays(p1_resultSet),p1_head);
            table1.setFont(f4);
            table1.setRowHeight(20);
            scrollPane1.setViewportView(table1);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }

        //p2组件
        admin = adminDAOimple.selectByAccount(acc);
        account.setText(admin.getAccount());
        name.setText(admin.getName());
        phone.setText(admin.getPhone());
        email.setText(admin.getEmail());
        password.setText(admin.getPassword());

        //p3组件
        ResultSet p3_resultSet;
        try {
            p3_resultSet = DataUtil.querySet(p3_sql,null);
            table3 = new JTable(DataUtil.getArrays(p3_resultSet), p3_head);
            table3.setFont(f4);
            table3.setRowHeight(20);
            scrollPane3.setViewportView(table3);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        try {
            p3_num1 = baseDAO.executeCount(p3_sql4,null);
            p3_num2 = baseDAO.executeCount(p3_sql5, null);
            p3_num3 = baseDAO.executeCount(p3_sql6, null);
            p3_num4 = baseDAO.executeCount(p3_sql7, null);
            p3_label3.setText("总用户数: "+p3_num1);
            p3_label4.setText("可用会议室数量: "+p3_num2);
            p3_label5.setText("预约数量: "+p3_num3);
            p3_label6.setText("待审核数: "+p3_num4);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }

        //p4组件
        ResultSet p4_resultSet;
        try {
            p4_resultSet = DataUtil.querySet(p4_sql,null);
            table4 = new JTable(DataUtil.getArrays(p4_resultSet), p4_head);
            table4.setFont(f4);
            table4.setRowHeight(20);
            scrollPane4.setViewportView(table4);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }

        //p5组件
        ResultSet p5_resultSet;
        try {
            p5_resultSet = DataUtil.querySet(p5_sql,null);
            table5 = new JTable(DataUtil.getArrays(p5_resultSet), p5_head);
            table5.setFont(f4);
            table5.setRowHeight(20);
            scrollPane5.setViewportView(table5);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public void mouseClicked(MouseEvent e) {
        Object obj = e.getSource();
        if (obj == p1_button3) {
            p1_roomSearch();
        }else if (obj == p1_button1) {
            //审核通过会议预约
            int selectedRow = table1.getSelectedRow();
            Object userName = table1.getValueAt(selectedRow, 0);
            Object roomName = table1.getValueAt(selectedRow, 1);
            User newUser = userDAOimple.selectByName((String) userName);
            Room newRoom = roomDAOimple.selectByName((String) roomName);
            System.out.println("userName = "+ newUser.getId());
            System.out.println("roomName = "+ newRoom.getId());
            int i = baseDAO.executeUpdate(p1_sql3,newUser.getId(),newRoom.getId());
//            int j = baseDAO.executeUpdate(p1_sql3_2, null);
            if (i > 0)
                baseDAO.executeUpdate(p1_sql4,newUser.getRealName(),newRoom.getName());
            this.InitTable(admin.getAccount());
        }else if (obj == p1_button2){
            //会议预约不通过
            int selectedRow = table1.getSelectedRow();
            Object userName = table1.getValueAt(selectedRow, 0);
            Object roomName = table1.getValueAt(selectedRow, 1);
            User newUser = userDAOimple.selectByName((String) userName);
            Room newRoom = roomDAOimple.selectByName((String) roomName);
            System.out.println("userName = "+ newUser.getId());
            System.out.println("roomName = "+ newRoom.getId());
            int i = baseDAO.executeUpdate(p1_sql5,newUser.getId(),newRoom.getId());
            if (i > 0)
                baseDAO.executeUpdate(p1_sql6,newUser.getRealName(),newRoom.getName());
            this.InitTable(admin.getAccount());
        }else if(obj == save){
            Admin newAdmin = new Admin();
            newAdmin.setId(admin.getId());
            newAdmin.setAccount(account.getText());
            newAdmin.setName(name.getText());
            newAdmin.setPhone(phone.getText());
            newAdmin.setEmail(email.getText());
            newAdmin.setPassword(password.getText());

            System.out.println(newAdmin);
            int i = adminDAOimple.update(newAdmin);
            if (i>0){
                System.out.println("修改成功！====="+newAdmin);
            }
        }else if(obj == quit){
            new Login();
            this.dispose();
        }else if(obj == p3_button1){
            p3_roomSearch();
        }else if(obj == p3_button2){
            p3_selectAll();
        }else if(obj == p3_button3){
            int[] selectedRows = table3.getSelectedRows();
            for (int i = 0; i < selectedRows.length; i++) {
                Object recordId = table3.getValueAt(selectedRows[i], 0);
                int delete = baseDAO.executeUpdate(p3_sql3, recordId);
                if (delete > 0){
                    System.out.println("删除成功");
                }else {
                    System.out.println("删除失败");
                }
            }
            this.InitTable(admin.getAccount());
        }else if (obj == p4_button1){
            p4_userSearch();
        }else if(obj == p4_button2){
            insertUser();
        }else if(obj == p4_button3){
            int selectedRow = table4.getSelectedRow();
            Object userId = table4.getValueAt(selectedRow, 0);
            int delete = baseDAO.executeUpdate(p4_sql3, userId);
            if (delete > 0){
                System.out.println("删除成功");
            }else {
                System.out.println("删除失败");
            }
            this.InitTable(admin.getAccount());
        }else if (obj == yes){
            accountRule.setVisible(false);
            passwordRule.setVisible(false);
            phoneRule.setVisible(false);
            emailRule.setVisible(false);
            if(RegexCheck()){
                if(isAllFilled()){
                    User user = userDAOimple.selectByAccount(accountField.getText());
                    if (user == null){
                        user = new User();
                        user.setAccount(accountField.getText());
                        user.setPassword(passwordField.getText());
                        user.setRealName(realNameField.getText());
                        user.setApartment(apartmentField.getText());
                        user.setDuty(dutyField.getText());
                        user.setPhone(phoneField.getText());
                        user.setEmail(emailField.getText());
                        int insert = userDAOimple.insert(user);
                        if (insert > 0){
                            System.out.println("新增用户成功");
                        }else {
                            popWin("发生错误");
                            return;
                        }
                    }else{
                        popWin("账号重复！");
                        return;
                    }
                }else {
                    popWin("请将用户信息补充完整！");
                    return;
                    //请填满信息
                }
            }else{
                //报错
                return;
            }
            this.InitTable(admin.getAccount());
            insertJDialog.dispose();
        }else if (obj == no){
            accountRule.setVisible(false);
            passwordRule.setVisible(false);
            phoneRule.setVisible(false);
            emailRule.setVisible(false);
            insertJDialog.dispose();
        }else if (obj == p5_button1){
            p5_roomSearch();
        }else if(obj == p5_button2){
            p5_updateStatus(p5_sql3);
        }else if (obj == p5_button3){
            p5_updateStatus(p5_sql4);
        }else if(obj == p5_button4){
            addRoom();
        }else if(obj == room_yes){
            if(roomIsAllFilled()){
                Room room = roomDAOimple.selectByName(roomNameField.getText());
                if (room==null){
                    room = new Room();
                    room.setName(roomNameField.getText());
                    room.setAddress(addressField.getText());
                    room.setSeatNum(Integer.parseInt(seatNumField.getText()));
                    room.setStatus("空闲");
                    int insert = roomDAOimple.insert(room);
                    if (insert > 0){
                        System.out.println("新增会议室成功");
                    }else {
                        popWin("发生错误");
                        return;
                    }
                }else {
                    popWin("会议室重复");
                    return;
                }
            }else{
                popWin("请将用户信息补充完整！");
                return;
            }
            this.InitTable(admin.getAccount());
            addRoomJDialog.dispose();
        }else if(obj == room_no){
            addRoomJDialog.dispose();
        }
    }

    private void p1_roomSearch() {
        String find = p1_text.getText();
        ResultSet resultSet;
        if ("".equals(find)){
            //显示全部会议信息
            try {
                resultSet = DataUtil.querySet(p1_sql, null);
                table1 = new JTable(DataUtil.getArrays(resultSet),p1_head);
                table1.setFont(f4);
                table1.setRowHeight(20);
                scrollPane1.setViewportView(table1);
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
            System.out.println("显示全部====================");
        }else {
            //显示部分
            try {
                resultSet = DataUtil.querySet(p1_sql2, "%"+find+"%");
                if (resultSet==null){
                    System.out.println("未找到");
                    return;
                }
                table1 = new JTable(DataUtil.getArrays(resultSet),p1_head);
                table1.setFont(f4);
                table1.setRowHeight(20);
                scrollPane1.setViewportView(table1);
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        }
    }

    private void p3_roomSearch() {
        String find = p3_text.getText();
        ResultSet resultSet;
        if ("".equals(find)){
            //显示全部会议信息
            try {
                resultSet = DataUtil.querySet(p3_sql, null);
                table3 = new JTable(DataUtil.getArrays(resultSet),p3_head);
                table3.setFont(f4);
                table3.setRowHeight(20);
                scrollPane3.setViewportView(table3);
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
            System.out.println("显示全部====================");
        }else {
            try {
                resultSet = DataUtil.querySet(p3_sql2, "%"+find+"%");
                if (resultSet==null){
                    System.out.println("未找到");
                    return;
                }
                table3 = new JTable(DataUtil.getArrays(resultSet),p3_head);
                table3.setFont(f4);
                table3.setRowHeight(20);
                scrollPane3.setViewportView(table3);
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        }
    }

    private void p3_selectAll() {
        int[] selectedRows = table3.getSelectedRows();
        int flag = 0;
        for (int i = 0; i < selectedRows.length; i++) {
            if(table3.isRowSelected(i))
                flag = 1;
        }
        if(flag == 0){
            table3.selectAll();
        }else{
            table3.clearSelection();
        }
    }

    private void p4_userSearch(){
        String find = p4_text.getText();
        ResultSet resultSet;
        if ("".equals(find)){
            //显示全部会议信息
            try {
                resultSet = DataUtil.querySet(p4_sql, null);
                table4 = new JTable(DataUtil.getArrays(resultSet),p4_head);
                table4.setFont(f4);
                table4.setRowHeight(20);
                scrollPane4.setViewportView(table4);
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
            System.out.println("显示全部====================");
        }else {
            try {
                resultSet = DataUtil.querySet(p4_sql2, "%"+find+"%");
                if (resultSet==null){
                    System.out.println("未找到");
                    return;
                }
                table4 = new JTable(DataUtil.getArrays(resultSet),p4_head);
                table4.setFont(f4);
                table4.setRowHeight(20);
                scrollPane4.setViewportView(table4);
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        }
    }

    private void insertUser(){
        insertJDialog = new JDialog();
        insertJDialog.setTitle("新增用户");
        insertJDialog.setSize(500, 500);
        insertJDialog.setLocationRelativeTo(null);
        insertJDialog.setLayout(null);
        insertJDialog.setAlwaysOnTop(true);
        insertJDialog.setResizable(false);

        JLabel accountLabel = new JLabel("账号");
        JLabel passwordLabel = new JLabel("密码");
        JLabel realNameLabel = new JLabel("姓名");
        JLabel apartmentLabel = new JLabel("部门");
        JLabel dutyLabel = new JLabel("职位");
        JLabel phoneLabel = new JLabel("手机号");
        JLabel emailLabel = new JLabel("邮箱");

        accountField.setFont(f2);
        passwordField.setFont(f2);
        realNameField.setFont(f2);
        apartmentField.setFont(f2);
        dutyField.setFont(f2);
        phoneField.setFont(f2);
        emailField.setFont(f2);

        accountLabel.setFont(f2);
        passwordLabel.setFont(f2);
        realNameLabel.setFont(f2);
        apartmentLabel.setFont(f2);
        dutyLabel.setFont(f2);
        phoneLabel.setFont(f2);
        emailLabel.setFont(f2);

        accountRule.setFont(f2);
        passwordRule.setFont(f2);
        phoneRule.setFont(f2);
        emailRule.setFont(f2);

        yes.setFont(f2);
        no.setFont(f2);

        accountLabel.setBounds(30, 10, 100, 50);
        passwordLabel.setBounds(30, 60, 100, 50);
        realNameLabel.setBounds(30, 110, 100, 50);
        apartmentLabel.setBounds(30, 160, 100, 50);
        dutyLabel.setBounds(30, 210, 100, 50);
        phoneLabel.setBounds(30, 260, 100, 50);
        emailLabel.setBounds(30, 310, 100, 50);

        accountField.setBounds(100, 20, 150, 30);
        passwordField.setBounds(100, 70, 150, 30);
        realNameField.setBounds(100, 120, 150, 30);
        apartmentField.setBounds(100, 170, 150, 30);
        dutyField.setBounds(100, 220, 150, 30);
        phoneField.setBounds(100, 270, 200, 25);
        emailField.setBounds(100, 320, 200, 25);

        accountRule.setBounds(260, 20, 150 ,25);
        passwordRule.setBounds(260, 70, 150 ,25);
        phoneRule.setBounds(320, 270, 200 ,25);
        emailRule.setBounds(320, 320, 150 ,25);

        accountRule.setForeground(Color.RED);
        passwordRule.setForeground(Color.RED);
        phoneRule.setForeground(Color.RED);
        emailRule.setForeground(Color.RED);
        accountRule.setVisible(false);
        passwordRule.setVisible(false);
        phoneRule.setVisible(false);
        emailRule.setVisible(false);

        accountField.setText("");
        passwordField.setText("");
        realNameField.setText("");
        apartmentField.setText("");
        dutyField.setText("");
        phoneField.setText("");
        emailField.setText("");

        yes.setBounds(30, 400, 100, 40);
        no.setBounds(220, 400, 100, 40);

        insertJDialog.add(accountField);
        insertJDialog.add(passwordField);
        insertJDialog.add(realNameField);
        insertJDialog.add(apartmentField);
        insertJDialog.add(dutyField);
        insertJDialog.add(phoneField);
        insertJDialog.add(emailField);

        insertJDialog.add(accountLabel);
        insertJDialog.add(passwordLabel);
        insertJDialog.add(realNameLabel);
        insertJDialog.add(apartmentLabel);
        insertJDialog.add(dutyLabel);
        insertJDialog.add(phoneLabel);
        insertJDialog.add(emailLabel);

        insertJDialog.add(accountRule);
        insertJDialog.add(passwordRule);
        insertJDialog.add(phoneRule);
        insertJDialog.add(emailRule);

        insertJDialog.add(yes);
        insertJDialog.add(no);

        insertJDialog.setModal(true);
        insertJDialog.setVisible(true);
    }

    private boolean RegexCheck() {
        String regex_acc = ".{3,}";
        //至少三位
        String regex_pas = ".{3,}";
        String regex_phone = "1\\d{10}";
        String regex_email = "\\w+@[\\w&&[^_]]+(\\.[a-zA-Z]{2,3})";
        int flag =0;
        if(!accountField.getText().matches(regex_acc)){
            flag=1;
            accountRule.setVisible(true);
        }
        if (!passwordField.getText().matches(regex_pas)){
            flag=1;
            passwordRule.setVisible(true);
        }
        if (!phoneField.getText().matches(regex_phone)){
            flag=1;
            phoneRule.setVisible(true);
        }
        if (!emailField.getText().matches(regex_email)){
            flag=1;
            emailRule.setVisible(true);
        }
        if (flag == 0){
            return true;
        }else {
            return false;
        }
    }

    private boolean isAllFilled(){
        if(!accountField.getText().isEmpty()){
            if (!passwordField.getText().isEmpty()){
                if (!phoneField.getText().isEmpty()){
                    if (!emailField.getText().isEmpty()){
                        if (!realNameField.getText().isEmpty()){
                            if (!apartmentField.getText().isEmpty()){
                                if (!dutyField.getText().isEmpty()){
                                    return true;
                                }
                            }
                        }
                    }
                }
            }
        }
        return false;
    }

    private void popWin(String str){
        JDialog dialog = new JDialog();
        dialog.setSize(350, 200);
        dialog.setAlwaysOnTop(true);
        dialog.setLocationRelativeTo(null);
        dialog.setLayout(null);
        JLabel label = new JLabel(str);
        label.setFont(f3);
        label.setBounds(70, 50, 300, 40);
        dialog.getContentPane().add(label);
        dialog.setModal(true);
        dialog.setVisible(true);
    }

    private void p5_roomSearch(){
        String find = p5_text.getText();
        ResultSet resultSet;
        if ("".equals(find)){
            //显示全部会议信息
            try {
                resultSet = DataUtil.querySet(p5_sql, null);
                table5 = new JTable(DataUtil.getArrays(resultSet),p5_head);
                table5.setFont(f4);
                table5.setRowHeight(20);
                scrollPane5.setViewportView(table5);
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
            System.out.println("显示全部====================");
        }else {
            try {
                resultSet = DataUtil.querySet(p5_sql2,"%"+find+"%");
                if (resultSet==null){
                    System.out.println("未找到");
                    return;
                }
                table5 = new JTable(DataUtil.getArrays(resultSet),p5_head);
                table5.setFont(f4);
                table5.setRowHeight(20);
                scrollPane5.setViewportView(table5);
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        }
    }

    private void p5_updateStatus(String sql) {
        int selectedRow = table5.getSelectedRow();
        Object roomId = table5.getValueAt(selectedRow, 0);
        int update = baseDAO.executeUpdate(sql, roomId);
        if (update > 0){
            System.out.println("设置成功！");
        }else {
            System.out.println("设置失败！");
        }
        this.InitTable(admin.getAccount());
    }

    private void addRoom(){
        addRoomJDialog = new JDialog();
        addRoomJDialog.setTitle("新增会议室");
        addRoomJDialog.setSize(400, 350);
        addRoomJDialog.setLocationRelativeTo(null);
        addRoomJDialog.setLayout(null);
        addRoomJDialog.setAlwaysOnTop(true);
        addRoomJDialog.setResizable(false);

        JLabel nameLabel = new JLabel("会议室名称");
        JLabel addressLabel = new JLabel("地点");
        JLabel seatNumLabel = new JLabel("座位数");
        JLabel statusLabel = new JLabel("状态");

        roomNameField.setFont(f2);
        addressField.setFont(f2);
        seatNumField.setFont(f2);
        statusField.setFont(f2);
        statusField.setEditable(false);

        nameLabel.setFont(f2);
        addressLabel.setFont(f2);
        seatNumLabel.setFont(f2);
        statusLabel.setFont(f2);

        room_yes.setFont(f2);
        room_no.setFont(f2);

        nameLabel.setBounds(30, 10, 100, 50);
        addressLabel.setBounds(30, 60, 100, 50);
        seatNumLabel.setBounds(30, 110, 100, 50);
        statusLabel.setBounds(30, 160, 100, 50);

        roomNameField.setBounds(150, 20, 150, 30);
        addressField.setBounds(150, 70, 150, 30);
        seatNumField.setBounds(150, 120, 150, 30);
        statusField.setBounds(150, 170, 150, 30);

        roomNameField.setText("");
        addressField.setText("");
        seatNumField.setText("");

        room_yes.setBounds(30, 250, 100, 40);
        room_no.setBounds(220, 250, 100, 40);
        room_yes.addMouseListener(this);
        room_no.addMouseListener(this);

        addRoomJDialog.add(roomNameField);
        addRoomJDialog.add(addressField);
        addRoomJDialog.add(seatNumField);
        addRoomJDialog.add(statusField);

        addRoomJDialog.add(nameLabel);
        addRoomJDialog.add(addressLabel);
        addRoomJDialog.add(seatNumLabel);
        addRoomJDialog.add(statusLabel);

        addRoomJDialog.add(room_yes);
        addRoomJDialog.add(room_no);

        addRoomJDialog.setModal(true);
        addRoomJDialog.setVisible(true);
    }

    private boolean roomIsAllFilled(){
        if (!roomNameField.getText().isEmpty()){
            if(!addressField.getText().isEmpty()){
                if(!seatNumField.getText().isEmpty()){
                    return true;
                }
            }
        }
        return false;
    }

    @Override
    public void mousePressed(MouseEvent e) {

    }

    @Override
    public void mouseReleased(MouseEvent e) {

    }

    @Override
    public void mouseEntered(MouseEvent e) {

    }

    @Override
    public void mouseExited(MouseEvent e) {

    }
}
